home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / imageresize.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  2.6 KB  |  74 lines

  1. # Miro - an RSS based video player application
  2. # Copyright (C) 2005-2007 Participatory Culture Foundation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18. """Code to handle resizing images.  """
  19.  
  20. import logging
  21. import os
  22. import traceback
  23.  
  24. from platformutils import resizeImage
  25.  
  26. def _resizedKey(width, height):
  27.     return u'%sx%s' % (width, height)
  28.  
  29. def _makeResizedPath(filename, width, height):
  30.     path, ext = os.path.splitext(filename)
  31.     path += '.%sx%s' % (width, height)
  32.     return path + ext
  33.  
  34. def multiResizeImage(source_filename, sizes):
  35.     """Resize an image to several sizes.
  36.  
  37.     Arguments:
  38.         source_filename -- image to resize
  39.         sizes -- list of (width, height) tuples to resize to.
  40.     
  41.     Returns a dict storing the images successfully resized.  The keys are
  42.     "<width>x<height>" and the values are the paths to the image.
  43.     """
  44.  
  45.     results = {}
  46.     for width, height in sizes:
  47.         resizedPath = _makeResizedPath(source_filename, width, height)
  48.         try:
  49.             resizeImage(source_filename, resizedPath, width, height)
  50.         except:
  51.             logging.warn("Error resizing %s to %sx%s:\n%s", source_filename,
  52.                     width, height, traceback.format_exc())
  53.         else:
  54.             results[_resizedKey(width, height)] = resizedPath
  55.     return results
  56.  
  57. def getImage(resized_filenames, width, height):
  58.     """Fetch a image from the results of multiResizeImage().  If (width,
  59.     height) wasn't one of the combinations passed to multiResizeImage(), or
  60.     the image wasn't successfully resized, a KeyError will be thrown.
  61.     """
  62.     return resized_filenames[_resizedKey(width, height)]
  63.  
  64. def removeResizedFiles(resized_filenames):
  65.     """Delete the files returned by multiResizeImage()."""
  66.  
  67.     for filename in resized_filenames.values():
  68.         try:
  69.             if (os.path.exists(filename)):
  70.                 os.remove (filename)
  71.         except:
  72.             logging.warn("Error deleted resized image: %s\n%s", filename,
  73.                     traceback.format_exc())
  74.